package com.issac.messagequeue.mqserver.coreclass;

import lombok.Data;

import java.io.Serializable;
import java.util.UUID;

/**
 * 表示一条消息
 */
@Data
public class Message implements Serializable {
    private static final long serialVersionUID = 1L;
    // 属性
    private BasicProperties basicProperties = new BasicProperties();
    // 正文
    private byte[] body;
    // offsetBeg：内容开始位置
    // offsetEnd：内容结束位置
    // [offsetBeg, offsetEnd)
    private transient long offsetBeg = 0;
    private transient long offsetEnd = 0;
    // 是否合法：0x1-有效-未被逻辑删除；0x0-无效-已被逻辑删除
    private byte isValid = 0x1;

    // 创建一个工厂方法，让工厂方法封装 Message 对象的创建过程
    public static Message createMessageWithId(BasicProperties basicProperties, byte[] body, String routingKey) {
        Message message = new Message();
        if (basicProperties != null) {
            message.setBasicProperties(basicProperties);
        }
        // 生成的messageId以M-为前缀
        message.setMessageId("M-" + UUID.randomUUID());
        message.setRoutingKey(routingKey);
        message.body = body;
        return message;
    }

    public String getMessageId() {
        return basicProperties.getMessageId();
    }

    public void setMessageId(String messageId) {
        basicProperties.setMessageId(messageId);
    }

    public String getRoutingKey() {
        return basicProperties.getRoutingKey();
    }

    public void setRoutingKey(String routingKey) {
        basicProperties.setRoutingKey(routingKey);
    }

    public int getDeliverMode() {
        return basicProperties.getDeliverMode();
    }

    public void setDeliverMode(int deliverMode) {
        basicProperties.setDeliverMode(deliverMode);
    }

    public BasicProperties getBasicProperties() {
        return basicProperties;
    }

    public void setBasicProperties(BasicProperties basicProperties) {
        this.basicProperties = basicProperties;
    }
}
